﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using MathWorks.MATLAB.NET.Arrays;

namespace EnerForecast
{
    class FunctionFitter
    {
        public int TempCostFit()
        {
            ArrayList ListX = new ArrayList();
            ArrayList ListY = new ArrayList();
           // GetTempCostData(ListX, ListY); //leng
            GetTempCostDataHeat(ListX, ListY); //热
            //转换成数组
            Double[] Y = (Double[])ListY.ToArray(typeof(Double));
            Double[] X = (Double[])ListX.ToArray(typeof(Double));
            //调用matlab生成的Fit.dll
            PolyFit.PolyFit pf = new PolyFit.PolyFit();

            MWArray resOut = pf.FFtest((MWNumericArray)X, (MWNumericArray)Y,3);
            MWNumericArray temp = (MWNumericArray)resOut;
            Array coef = temp.ToArray(MWArrayComponent.Real);
           // SaveCoeftoDB(coef, 0);  //冷
            SaveCoeftoDB1(coef, 0);  //热
            return 1;
        }
        //按月拟合曲线
        public int CostHourFit(int month)
        {
            ArrayList ListX = new ArrayList();
            ArrayList ListY = new ArrayList();
            //拟合一点至七点
            GetCostHourData(ListX, ListY,true,month);
            //转换成数组
            Double[] Y = (Double[])ListY.ToArray(typeof(Double));
            int[] X = (int[])ListX.ToArray(typeof(int));
            //调用matlab生成的Fit.dll
            PolyFit.PolyFit pf = new PolyFit.PolyFit();

            MWArray resOut = pf.FFtest((MWNumericArray)X, (MWNumericArray)Y, 3);
            MWNumericArray temp = (MWNumericArray)resOut;
            Array coef = temp.ToArray(MWArrayComponent.Real);
            SaveCoeftoDB(coef, 2*month-1);
            
            //拟合八点至二十四点
            ListX.Clear();
            ListY.Clear();
            GetCostHourData(ListX, ListY,false,month);
            //转换成数组
            Y = (Double[])ListY.ToArray(typeof(Double));
            X = (int[])ListX.ToArray(typeof(int));
            //调用matlab生成的Fit.dll
            pf = new PolyFit.PolyFit();

            resOut = pf.FFtest((MWNumericArray)X, (MWNumericArray)Y, 3);
            temp = (MWNumericArray)resOut;
            coef = temp.ToArray(MWArrayComponent.Real);
            SaveCoeftoDB(coef, 2*month);


            return 1;
        }
        private void GetCostHourData(ArrayList X, ArrayList Y ,bool is17,int month)
        {
            ForeCastDataSet.tb_costPhourDataTable cost_tb = new ForeCastDataSet.tb_costPhourDataTable();
            ForeCastDataSetTableAdapters.tb_costPhourTableAdapter cost_adapter = new ForeCastDataSetTableAdapters.tb_costPhourTableAdapter();
            ///
            if (is17)
            {
                cost_adapter.FillDataBetween17(cost_tb, month);
            }
            else {
                cost_adapter.FillDataOver8(cost_tb, month);
            }
            foreach (DataRow i in cost_tb)
            {
                X.Add(i["Clock"]);
                Y.Add(i["Cost"]);
            }
            
        }

        private void GetTempCostData(ArrayList X, ArrayList Y)
        {
            ForeCastDataSet.tb_temp_costPdayDataTable tempcost_tb = new ForeCastDataSet.tb_temp_costPdayDataTable();
            ForeCastDataSetTableAdapters.tb_temp_costPdayTableAdapter tempcost_ad = new ForeCastDataSetTableAdapters.tb_temp_costPdayTableAdapter();
            tempcost_ad.Fill(tempcost_tb);
            foreach (DataRow i in tempcost_tb)
            {
                X.Add(i["Ltemp"]);
                Y.Add(i["Cost"]);
            }
        }  //冷

        private void GetTempCostDataHeat(ArrayList X, ArrayList Y)
        {
            ForeCastDataSet.tb_temp_costPday1DataTable tempcost_tb = new ForeCastDataSet.tb_temp_costPday1DataTable();
            ForeCastDataSetTableAdapters.tb_temp_costPday1TableAdapter tempcost_ad = new ForeCastDataSetTableAdapters.tb_temp_costPday1TableAdapter();
            tempcost_ad.Fill(tempcost_tb);
            foreach (DataRow i in tempcost_tb)
            {
                X.Add(i["Ltemp"]);
                Y.Add(i["Cost"]);
            }
        }  //热
        private void SaveCoeftoDB(Array coef, int index)
        {
            ForeCastDataSetTableAdapters.tb_coefficientTableAdapter coadapter = new ForeCastDataSetTableAdapters.tb_coefficientTableAdapter();
            ForeCastDataSet.tb_coefficientDataTable cotb = new ForeCastDataSet.tb_coefficientDataTable();
            cotb = coadapter.GetDataByID(index);
            if (cotb.Rows.Count != 0)
            {
                coadapter.UpdateQueryWithoutID((Double)coef.GetValue(0,0), (Double)coef.GetValue(0,1), (Double)coef.GetValue(0,2), (Double)coef.GetValue(0,3), index);
            }
            else
            {
                coadapter.InsertQuery((Double)coef.GetValue(0,0), (Double)coef.GetValue(0,1), (Double)coef.GetValue(0,2), (Double)coef.GetValue(0,3), index);
            }
        }

        public void DeviceCostFit(DataSet ds, int type) {
            DataTable dt = ds.Tables[0];
            for (int i = 2; i <= 10; i++)
            {
                DataRow[] rows = dt.Select("Pct = " + (i * 10).ToString());

                ArrayList ListX = new ArrayList();
                ArrayList ListY = new ArrayList();
                //取值
                foreach (DataRow row in rows)
                {
                    ListX.Add(row["CLFT"]);
                    ListY.Add(row["Inp Pwr"]);
                }
                //转化

                Double[] X = (Double[])ListX.ToArray(typeof(Double));
                Double[] Y = (Double[])ListY.ToArray(typeof(Double));
                //调用matlab生成的Fit.dll
                PolyFit.PolyFit pf = new PolyFit.PolyFit();

                MWArray resOut = pf.FFtest((MWNumericArray)X, (MWNumericArray)Y, 3);
                MWNumericArray temp = (MWNumericArray)resOut;
                Array coef = temp.ToArray(MWArrayComponent.Real);

                ForeCastDataSetTableAdapters.tb_deviceCOPTableAdapter deviAdapter = new ForeCastDataSetTableAdapters.tb_deviceCOPTableAdapter();
                ForeCastDataSet.tb_deviceCOPDataTable deviTable = new ForeCastDataSet.tb_deviceCOPDataTable();
                deviTable = deviAdapter.GetDataByPCTnTYPE((i * 10).ToString(), type);
                if (deviTable.Rows.Count != 0)
                {
                    deviAdapter.UpdateQuery((Double)coef.GetValue(0, 0), (Double)coef.GetValue(0, 1), (Double)coef.GetValue(0, 2), (Double)coef.GetValue(0, 3), (i * 10).ToString(), type);
                }
                else
                {
                    deviAdapter.InsertQuery((i * 10).ToString(), type, (Double)coef.GetValue(0, 0), (Double)coef.GetValue(0, 1), (Double)coef.GetValue(0, 2), (Double)coef.GetValue(0, 3));
                }
            }
        }


        public void IcingFit(DataSet ds)
        {
            DataTable dt = ds.Tables[0];
            ArrayList ListX = new ArrayList();
            ArrayList ListY = new ArrayList();
            ArrayList ListZ = new ArrayList();
            //取值
            foreach (DataRow row in dt.Rows)
            {
                ListX.Add(row["CLT"]);
                ListY.Add(row["制冷量kW"]);
                ListZ.Add(row["COP"]);
            }
            //转化

            Double[] X = (Double[])ListX.ToArray(typeof(Double));
            Double[] Y = (Double[])ListY.ToArray(typeof(Double));
            Double[] Z = (Double[])ListZ.ToArray(typeof(Double));
            //调用matlab生成的Fit.dll
            PolyFit.PolyFit pf = new PolyFit.PolyFit();

            MWArray resOut1 = pf.FFtest((MWNumericArray)X, (MWNumericArray)Y, 3);//制冷量关于温度
            MWNumericArray temp1 = (MWNumericArray)resOut1;
            Array coef1 = temp1.ToArray(MWArrayComponent.Real);
            MWArray resOut2 = pf.FFtest((MWNumericArray)X, (MWNumericArray)Z, 3);//COP关于温度
            MWNumericArray temp2 = (MWNumericArray)resOut2;
            Array coef2 = temp2.ToArray(MWArrayComponent.Real);

            ForeCastDataSetTableAdapters.tb_deviceCOPTableAdapter deviAdapter = new ForeCastDataSetTableAdapters.tb_deviceCOPTableAdapter();
            ForeCastDataSet.tb_deviceCOPDataTable deviTable = new ForeCastDataSet.tb_deviceCOPDataTable();
            deviTable = deviAdapter.GetDataByPCTnTYPE("100", 3);//更新制冷量关于温度
            if (deviTable.Rows.Count != 0)
            {
                deviAdapter.UpdateQuery((Double)coef1.GetValue(0, 0), (Double)coef1.GetValue(0, 1), (Double)coef1.GetValue(0, 2), (Double)coef1.GetValue(0, 3), "100", 3);
            }
            else
            {
                deviAdapter.InsertQuery("100", 3, (Double)coef1.GetValue(0, 0), (Double)coef1.GetValue(0, 1), (Double)coef1.GetValue(0, 2), (Double)coef1.GetValue(0, 3));
            }

            deviTable = deviAdapter.GetDataByPCTnTYPE("100", 4);//更新COP关于温度
            if (deviTable.Rows.Count != 0)
            {
                deviAdapter.UpdateQuery((Double)coef2.GetValue(0, 0), (Double)coef2.GetValue(0, 1), (Double)coef2.GetValue(0, 2), (Double)coef2.GetValue(0, 3), "100", 4);
            }
            else
            {
                deviAdapter.InsertQuery("100", 4, (Double)coef2.GetValue(0, 0), (Double)coef2.GetValue(0, 1), (Double)coef2.GetValue(0, 2), (Double)coef2.GetValue(0, 3));
            }
        }
        public int CostHourFit1(int month)     //re
        {
            ArrayList ListX = new ArrayList();
            ArrayList ListY = new ArrayList();
            //拟合一点至七点
            GetCostHourData1(ListX, ListY, true, month);
            //转换成数组
            Double[] Y = (Double[])ListY.ToArray(typeof(Double));
            int[] X = (int[])ListX.ToArray(typeof(int));
            //调用matlab生成的Fit.dll
            PolyFit.PolyFit pf = new PolyFit.PolyFit();

            MWArray resOut = pf.FFtest((MWNumericArray)X, (MWNumericArray)Y, 3);
            MWNumericArray temp = (MWNumericArray)resOut;
            Array coef = temp.ToArray(MWArrayComponent.Real);
            SaveCoeftoDB1(coef, 2 * month - 1);

            //拟合八点至二十四点
            ListX.Clear();
            ListY.Clear();
            GetCostHourData1(ListX, ListY, false, month);
            //转换成数组
            Y = (Double[])ListY.ToArray(typeof(Double));
            X = (int[])ListX.ToArray(typeof(int));
            //调用matlab生成的Fit.dll
            pf = new PolyFit.PolyFit();

            resOut = pf.FFtest((MWNumericArray)X, (MWNumericArray)Y, 3);
            temp = (MWNumericArray)resOut;
            coef = temp.ToArray(MWArrayComponent.Real);
            SaveCoeftoDB1(coef, 2 * month);


            return 1;
        }
        private void GetCostHourData1(ArrayList X, ArrayList Y, bool is17, int month)  //re
        {
            ForeCastDataSet.tb_costPhour1DataTable cost_tb = new ForeCastDataSet.tb_costPhour1DataTable();
            ForeCastDataSetTableAdapters.tb_costPhour1TableAdapter cost_adapter = new ForeCastDataSetTableAdapters.tb_costPhour1TableAdapter();
            ///
            if (is17)
            {
                cost_adapter.FillDataBetween17(cost_tb, month);
            }
            else
            {
                cost_adapter.FillDataOver8(cost_tb, month);
            }
            foreach (DataRow i in cost_tb)
            {
                X.Add(i["Clock"]);
                Y.Add(i["Cost"]);
            }

        }

        private void SaveCoeftoDB1(Array coef, int index) //re
        {
            ForeCastDataSetTableAdapters.tb_coefficient1TableAdapter coadapter = new ForeCastDataSetTableAdapters.tb_coefficient1TableAdapter();
            ForeCastDataSet.tb_coefficient1DataTable cotb = new ForeCastDataSet.tb_coefficient1DataTable();
            cotb = coadapter.GetDataByID(index);
            if (cotb.Rows.Count != 0)
            {
                coadapter.UpdateQueryWithoutID((Double)coef.GetValue(0, 0), (Double)coef.GetValue(0, 1), (Double)coef.GetValue(0, 2), (Double)coef.GetValue(0, 3), index);
            }
            else
            {
                coadapter.InsertQuery((Double)coef.GetValue(0, 0), (Double)coef.GetValue(0, 1), (Double)coef.GetValue(0, 2), (Double)coef.GetValue(0, 3), index);
            }
        }
    }
}
